﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TripLOAMobile.WS_SoapLogin;
using TripLOAMobile.InternalEntities;
using CompactFramework.Utilities.Localization;



//using System.IO;

using System.Collections;

namespace TripLOAMobile
{
    public partial class ViewNotifications : LocalizedForm
    {
       
        private ListViewTravelControl notifications = null;
        private WS_SoapLogin.Notification[] ntf = null;
        private AnimationControl animation;
        private bool menuItemRight;
        //private bool menuItemLeft;

        public ViewNotifications()
        {
            #region creation of the control for the animation
            animation = new AnimationControl();
            int x = this.Width / 2 - animation.Width / 2;
            int y = this.Height / 2 - animation.Height / 2;
            animation.Location = new Point(x, y);
            this.Controls.Add(animation);
            animation.BringToFront();
            #endregion

            notifications = new ListViewTravelControl(0);
            this.SuspendLayout();
            this.notifications.Location = new System.Drawing.Point(15, 15);
            this.notifications.Name = "listViewTravel";
            this.notifications.Size = new System.Drawing.Size(210, 240);
            this.notifications.TabIndex = 0;
            this.Controls.Add(this.notifications);
            this.ResumeLayout(false);
            this.notifications.Section();
            InitializeComponent();
            this.AddMenusToDictionary();
            UpdateResources();
            this.startAnimation();
            this.GetNotification();
            this.menuItem2.Enabled = false;
        }
       
        protected override void AddMenusToDictionary()
        {
            this.AddMenuToDictionary(this.menuLeft, "menuLeft");
            this.AddMenuToDictionary(this.menuRight, "menuRight");
            this.AddMenuToDictionary(this.menuItem1, "menuBack");
            this.AddMenuToDictionary(this.menuItem2, "menuHome");
        }




        public Hashtable ToHashtable(object[][] obj)
        {
            Hashtable ht = new Hashtable(obj.Length);
            foreach (object[] pair in obj)
            {
                object key = pair[0];
                object value = pair[1];
                ht[key] = value;
            }
            return ht;
        }

        internal Notification [] ListOfNotification
        {
            set
            {
                if ((value == null))
                {
                    this.menuRight.Enabled = false;
                }

                else
                {
                    string[] linksTravel = new string[value.Length];
                    string[] commentTravel = new string[value.Length];
                    int i = 0;

                    //da reimplementare
                    foreach (Notification n in value)
                    {
                        Hashtable t = this.ToHashtable(n.Fields);
                        //object[][] obj = n.Fields;
                        //int g = obj.Length;   
                        //linksTravel[i] = n.TemplateType.ToString()+" "+n.SenderID.ToString(); //"+n.Fields.ElementAt[0];"
                        //commentTravel[i] = "Notification "+(i+1);
                        //i++;
                    }

                    this.notifications.Notifications(linksTravel, commentTravel);

                    if ((value.Length == 0 || this.notifications.NowIsInTheLastPage() || (this.notifications.NowIsInTheLastPage() && this.notifications.PageNotificationIsFull())))
                    {
                        if (value.Length == 0)
                        {
                            MessageBox.Show(ResourceManager.Instance.GetString("EmptyNotificationList"));
                        }
                        this.menuRight.Enabled = false;
                        this.menuItemRight = this.menuRight.Enabled;
                    }
                    else
                    {
                        this.menuRight.Enabled = true;
                        this.menuItemRight = this.menuRight.Enabled;
                    }

                }
            }
        }

        #region responseHandling
        private void rrp_NotificationResponse(RemoteResourceProvider.NotificationListResposeEventArgs e)
        {
            this.stopAnimation();
            if ((e.Status == RemoteResourceProvider.okStatus))
            {
                this.ntf = e.Results;
                this.ListOfNotification = ntf;
            }
            else
            {
                //MessageBox.Show(ResourceManager.Instance.GetString("nessuna notifica presente"));
                MessageBox.Show(ResourceManager.Instance.GetString(e.Status));
            }
        }


        public void GetNotification()
        {
            Welcome.rrp.GetNotificationList(this, rrp_NotificationResponse, this.notifications.ActualNumberPage);
        }
        #endregion


        #region start/stop animation
        delegate void animationDel();
        private void stopAnimation()
        {
            foreach (Control c in this.Controls)
            {
                c.Enabled = true;
            }

            
            this.menuLeft.Enabled = true;
            if (this.menuItemRight)
            {
                this.menuRight.Enabled = true;
            }
            this.animation.StopAnimation();

        }
        private void startAnimation()
        {
            foreach (Control c in this.Controls)
            {
                c.Enabled = false;
            }
            this.menuLeft.Enabled = false;
            this.menuRight.Enabled = false;
            this.animation.StartAnimation();

        }
        #endregion

        private void menuRight_Click(object sender, EventArgs e)
        {
            if (this.notifications.ActualNumberPage == 0 && this.notifications.TravelsInPage())
            {
                this.menuItem2.Enabled = true;
                this.menuItem1.Text = ResourceManager.Instance.GetString("ShowPrev");
            }

            //if (this.notifications.NowIsInTheLastPage())
            //{
            //    animation.StartAnimation();
            //    this

            //}
            else
            {
                this.notifications.GoNextNotification();
                if (this.notifications.NowIsInTheLastPage())
                    this.menuRight.Enabled = false;
            }
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {//il back è da controllare
            this.menuRight.Enabled = true;
            if (this.notifications.ActualNumberPage < 1)
                Welcome.formBuffer.Close();
           
            else
            {
                if (this.notifications.ActualNumberPage == 1)
                {
                    this.menuItem1.Text = ResourceManager.Instance.GetString("ViewNotifications_menuBack");
                    menuItem2.Enabled = false;
                }
                this.notifications.GoBackNotification();
            }
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            Welcome.formBuffer.Clear();
        }
    }
}